<?php
declare(strict_types=1);

namespace Core\Service;

use Core\Package\Attribute\Describe;
use Zms\Exception\AppException;
use Zms\Sms\Drive\Config\Fxconfig;
use Zms\Sms\Drive\Config\Ztconfig;
use Zms\Sms\Drive\SmsBasics;
use Zms\Sms\Drive\Ztsms;
use Zms\Sms\Sms;
use Zms\Sms\Drive\Config\Alconfig;
use Zms\Sms\Drive\Alsms;
use App\Common\Modular\System\Serve\{SmsTemplateServe};

#[Describe(title: "短信服务")]
class SmsService
{

    /**
     *['KEY' => 'SMST-DEFAULT', 'TEMPLATE_ID' => 'SMS_284970217', 'DESCRIPTION' => '短信验证码', 'CONTENT' => '【{signature}】您的验证码为：{code}，请勿泄露于他人！']
     * 短信实例获取
     * @return SmsBasics
     * @throws AppException
     */
    public function instance(): SmsBasics
    {
        $configure = configure('sms');

        $template = container(SmsTemplateServe::class)->getSmsTemplateList();

        $sms = new Sms;

        /**
         * 获取短信实例
         */
        return $sms->instance($configure['driver'], function (string $drive) use ($configure, $template) {
            if ($drive === Alsms::class) {
                //阿里云短信
                return new Alconfig($configure['AccessKeyId'], $configure['AccessKeySecret'], $configure['sign'], $template);
            } elseif ($drive === Ztsms::class) {
                //助通短信科技
                return new Ztconfig($configure['username'], $configure['password'], $configure['sign'], $template);
            }
            //分享短信
            return new Fxconfig($configure['username'], $configure['password'], $configure['sign'], $template);
        });
    }
}